import { ApiProperty } from '@nestjs/swagger'
import { IsNumber, IsOptional, IsString } from 'class-validator'

/**
 * 大屏统计查询DTO
 */
export class DashboardQueryDto {
  @ApiProperty({ description: '统计开始日期', required: false, example: '2025-01-01' })
  @IsOptional()
  @IsString()
  startDate?: string

  @ApiProperty({ description: '统计结束日期', required: false, example: '2025-01-31' })
  @IsOptional()
  @IsString()
  endDate?: string

  @ApiProperty({ description: '部门ID', required: false })
  @IsOptional()
  @IsNumber()
  deptId?: number

  @ApiProperty({ description: '用户ID', required: false })
  @IsOptional()
  @IsNumber()
  userId?: number
}

/**
 * 大屏总览数据
 */
export class DashboardOverviewDto {
  @ApiProperty({ description: '总用户数' })
  totalUsers: number

  @ApiProperty({ description: '总设备数' })
  totalDevices: number

  @ApiProperty({ description: '在线设备数' })
  onlineDevices: number

  @ApiProperty({ description: '今日数据量' })
  todayDataCount: number

  @ApiProperty({ description: '今日告警数' })
  todayAlarmCount: number

  @ApiProperty({ description: '待处理告警数' })
  pendingAlarmCount: number

  @ApiProperty({ description: '今日SOS数' })
  todaySosCount: number

  @ApiProperty({ description: '待处理SOS数' })
  pendingSosCount: number

  @ApiProperty({ description: '活跃用户数（近7天）' })
  activeUsers: number
}

/**
 * 数据趋势点
 */
export class DataTrendPointDto {
  @ApiProperty({ description: '日期/时间' })
  date: string

  @ApiProperty({ description: '平均值' })
  avgValue: number

  @ApiProperty({ description: '最大值' })
  maxValue: number

  @ApiProperty({ description: '最小值' })
  minValue: number

  @ApiProperty({ description: '数据量' })
  count: number
}

/**
 * 健康数据趋势
 */
export class HealthDataTrendDto {
  @ApiProperty({ description: '数据类型', example: 1 })
  type: number

  @ApiProperty({ description: '数据类型名称', example: '心率' })
  typeName: string

  @ApiProperty({ description: '趋势数据', type: [DataTrendPointDto] })
  trend: DataTrendPointDto[]
}

/**
 * 告警统计
 */
export class AlarmStatDto {
  @ApiProperty({ description: '告警类型' })
  type: number

  @ApiProperty({ description: '告警类型名称' })
  typeName: string

  @ApiProperty({ description: '告警次数' })
  count: number

  @ApiProperty({ description: '占比（%）' })
  percentage: number
}

/**
 * 设备状态统计
 */
export class DeviceStatusStatDto {
  @ApiProperty({ description: '在线设备数' })
  online: number

  @ApiProperty({ description: '离线设备数' })
  offline: number

  @ApiProperty({ description: '告警设备数' })
  alarm: number

  @ApiProperty({ description: '正常设备数' })
  normal: number
}

/**
 * 地区分布统计
 */
export class RegionStatDto {
  @ApiProperty({ description: '地区名称' })
  region: string

  @ApiProperty({ description: '用户数' })
  userCount: number

  @ApiProperty({ description: '设备数' })
  deviceCount: number

  @ApiProperty({ description: '告警数' })
  alarmCount: number
}

/**
 * 最近告警记录
 */
export class RecentAlarmDto {
  @ApiProperty({ description: '告警ID' })
  id: number

  @ApiProperty({ description: '用户昵称' })
  userName: string

  @ApiProperty({ description: '告警类型' })
  type: number

  @ApiProperty({ description: '告警内容' })
  content: string

  @ApiProperty({ description: '告警状态' })
  status: number

  @ApiProperty({ description: '开始时间' })
  startAt: Date

  @ApiProperty({ description: '设备序列号' })
  deviceSn: string
}

/**
 * 大屏完整统计数据
 */
export class DashboardStatsDto {
  @ApiProperty({ description: '总览数据', type: DashboardOverviewDto })
  overview: DashboardOverviewDto

  @ApiProperty({ description: '健康数据趋势（近7天/30天）', type: [HealthDataTrendDto] })
  healthTrends: HealthDataTrendDto[]

  @ApiProperty({ description: '告警统计', type: [AlarmStatDto] })
  alarmStats: AlarmStatDto[]

  @ApiProperty({ description: '设备状态统计', type: DeviceStatusStatDto })
  deviceStatus: DeviceStatusStatDto

  @ApiProperty({ description: '部门分布统计', type: [RegionStatDto] })
  deptStats: RegionStatDto[]

  @ApiProperty({ description: '最近告警（前10条）', type: [RecentAlarmDto] })
  recentAlarms: RecentAlarmDto[]
}

